package com.yisu.expressway.onedollar.widget.addandsub;

import com.yisu.expressway.R;
import com.yisu.expressway.utils.SpannableUtil;
import com.yisu.expressway.utils.StringUtil;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by Kaven on 2016/12/12.
 */

public class AddAndSubView extends RelativeLayout {
    TextView mRemainTv;
    TextView mAddTv;
    TextView mSubTv;
    EditText mCountEdt;
    TextView mCount5Tv;
    TextView mCount10Tv;
    TextView mCount20Tv;
    private int mCount = 1;
    private int mRemain = 1;
    private int mTotal = 0;
	private double mPrice = 1;
    private OnCountChangedListener onCountChangedListener;

    public AddAndSubView(Context context) {
        this(context, null);
    }

    public AddAndSubView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public AddAndSubView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_add_sub, this, true);
        init();
    }
	public void setEnable(boolean enable){
		mAddTv.setEnabled(enable);
		mSubTv.setEnabled(enable);
		mCount5Tv.setEnabled(enable);
		mCount10Tv.setEnabled(enable);
		mCount20Tv.setEnabled(enable);
	}
    public void setRemain(int remain) {
        this.mRemain = remain;
        refreshView();
    }
    public void setTotal(int total) {
        this.mTotal = total;
        refreshView();
    }
	public void setPrice(double price){
		this.mPrice = price;
		refreshView();
	}

	public int getCount() {
		return mCount;
	}

	private void refreshView() {

        if (mCountEdt != null) {
            mCountEdt.setText(Integer.toString(mCount));
        }
        if (mRemainTv != null) {
            String format = String.format(getResources().getString(R.string.one_dollar_join_remain), mRemain - mCount);
            mRemainTv.setText(SpannableUtil.setHighLight(getContext(),format,format.substring(0,2),R.color.oneDollarTextGrey));
        }

	    if (mAmountTv != null){
		    mAmount = (double) mCount * mPrice;
		    String format = String.format(getResources().getString(R.string.one_dollar_amount),
			    Double.toString(mAmount));
		    mAmountTv.setText( SpannableUtil.setHighLight(getContext(),format,format.substring(0,2),R.color.oneDollarTextBlack));
	    }
        if (mCount == 5){
            mCount5Tv.setBackground(getResources().getDrawable(R.drawable.btn_selector_border_purple));
        }else {
            mCount5Tv.setBackground(getResources().getDrawable(R.drawable.btn_selector_border_grey));
        }
        if (mCount == 10){
            mCount10Tv.setBackground(getResources().getDrawable(R.drawable.btn_selector_border_purple));
        }else {
            mCount10Tv.setBackground(getResources().getDrawable(R.drawable.btn_selector_border_grey));
        }
        if (mCount == 20){
            mCount20Tv.setBackground(getResources().getDrawable(R.drawable.btn_selector_border_purple));
        }else {
            mCount20Tv.setBackground(getResources().getDrawable(R.drawable.btn_selector_border_grey));
        }
        invalidate();
    }
	private double mAmount;
	private void init() {
        mCount5Tv = (TextView) findViewById(R.id.tv_count_5);
        mCount5Tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mCount = mRemain >= 5? 5 : mRemain;
                refreshView();
            }
        });

        mCount10Tv = (TextView) findViewById(R.id.tv_count_10);
        mCount10Tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mCount = mRemain >= 10? 10 : mRemain;
                refreshView();
            }
        });
        mCount20Tv = (TextView) findViewById(R.id.tv_count_20);
        mCount20Tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mCount = mRemain >= 20? 20 : mRemain;
                refreshView();
            }
        });
        mRemainTv = (TextView) findViewById(R.id.tv_remain);
        mSubTv = (TextView) findViewById(R.id.tv_sub);
        mSubTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCount <=1){
                    return;
                }
                if (mCount > 0) {
                    mCount--;
                }
                refreshView();
            }
        });
        mAddTv = (TextView) findViewById(R.id.tv_add);
        mAddTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCount < mRemain) {
                    mCount++;
                }
                refreshView();
            }
        });
        mCountEdt = (EditText) findViewById(R.id.edt_count);
        mCountEdt.setFocusable(false);
        mCountEdt.setEnabled(false);
        mCountEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isNullOrEmpty(s.toString())) {
                    return;
                }
                int count1 = Integer.parseInt(s.toString());
                if (count1 > mRemain) {
                    mCount = mRemain;
                } else {
                    mCount = count1;
                }
                if (onCountChangedListener != null) {

                    onCountChangedListener.onCountChanged(mCount);
                }
                if (mRemainTv != null) {
                    String format = String.format(getResources().getString(R.string.one_dollar_join_remain),
                        mRemain - mCount);
                    mRemainTv.setText(SpannableUtil.setHighLight(getContext(),format,format.substring(0,2),R.color.oneDollarTextGrey));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
	    mAmountTv = (TextView) findViewById(R.id.tv_total_amount);
	    mBuyNowTv = (TextView) findViewById(R.id.tv_buy_now);
	    mBuyNowTv.setOnClickListener(new OnClickListener() {
		    @Override
		    public void onClick(View v) {
			    if (onCountChangedListener != null){
				    onCountChangedListener.onBunNowClicked(mAmount, mCount);
			    }
		    }
	    });
        refreshView();
    }
	private TextView mAmountTv;
	private TextView mBuyNowTv;
    public void setOnCountChangedListener(OnCountChangedListener onCountChangedListener) {
        this.onCountChangedListener = onCountChangedListener;
    }

    public interface OnCountChangedListener {
        void onCountChanged(int count);
	    void onBunNowClicked(double amount, int count);
    }

}
